import os
import pandas as pd

# 指定文件夹路径
#repo_name_list = ["cmssw","django","kubernetes","laravel","moby","opencv","pandas","rails","react","rust","salt","scikit-learn","symfony",
# "tensorflow","terraform","yii2"]
repo_name_list = ["yii2"]
for repo_name in repo_name_list:
    folder_path = "./sim_data/" + repo_name + "/result/"

    # 获取文件夹下所有文件名为"xxx_result.xlsx"的文件
    file_list = [file for file in os.listdir(folder_path) if file.endswith("_result.xlsx")]

    # 读取文件的第三行数据并输出到csv中
    output_data = []
    for file in file_list:
        file_path = os.path.join(folder_path, file)
        df = pd.read_excel(file_path)
        third_row = df.iloc[1]  # 获取第三行数据
        # 向third_row新增一列file_name
        third_row["file_name"] = file
        output_data.append(third_row)


    output_df = pd.concat(output_data, axis=1).T  # 将数据拼接并转置
    # 将output_df中的标题为file_name那一列放到第一列
    output_df = output_df[["file_name"] + [col for col in output_df.columns if col != "file_name"]]
    output_df.to_csv(folder_path+repo_name+"_output.csv", index=False)  # 输出到csv文件


# 指定文件夹路径
# repo_name_list = ["cmssw","django","kubernetes","laravel","moby","opencv","pandas","rails","react","rust","salt","scikit-learn","symfony","tensorflow"]
# for repo_name in repo_name_list:
#     folder_path = "./rank_model/" + repo_name + "/result/rank_eval/"
#
#     # 获取文件夹下所有文件名为"xxx_result.xlsx"的文件
#     file_list = [file for file in os.listdir(folder_path) if file.endswith("_sum_result.csv")]
#
#     # 读取文件的第三行数据并输出到csv中
#     output_data = []
#     for file in file_list:
#         file_path = os.path.join(folder_path, file)
#         df = pd.read_csv(file_path)
#         # 计算新列的值
#         df['Sum'] = df.iloc[:, -3] + df.iloc[:, -2] - df.iloc[:, -1]
#
#         # 保存修改后的CSV文件
#         df.to_csv(folder_path+repo_name+'_new_sum_result.csv', index=False)


